package com.toKnow.model.dto;

import com.toKnow.model.vo.RestrictionVo;
import com.toKnow.service.ServiceException;

import java.util.Date;
import java.util.List;

public class SearchDto {
    private Integer startRow;
    private Integer oneRowNum=10;
    private String keyword;
    private String category;
    private String type;
    private List<String> tag;
    private Integer maxWord;
    private Integer minWord;
    private Date expire;
    private Boolean file;
    private String sort;
    private Integer authorId;

    public SearchDto() {
    }

    @Override
    public String toString() {
        return "SearchDto{" +
                "startRow=" + startRow +
                ", oneRowNum=" + oneRowNum +
                ", keyword='" + keyword + '\'' +
                ", category='" + category + '\'' +
                ", type='" + type + '\'' +
                ", tag=" + tag +
                ", maxWord=" + maxWord +
                ", minWord=" + minWord +
                ", expire=" + expire +
                ", file=" + file +
                ", sort='" + sort + '\'' +
                ", authorId=" + authorId +
                '}';
    }

    public Integer getAuthorId() {
        return authorId;
    }

    public void setAuthorId(Integer authorId) {
        this.authorId = authorId;
    }

    public Integer getStartRow() {
        return startRow;
    }

    public void setStartRow(Integer startRow) {
        this.startRow = startRow;
    }

    public Integer getOneRowNum() {
        return oneRowNum;
    }

    public void setOneRowNum(Integer oneRowNum) {
        this.oneRowNum = oneRowNum;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<String> getTag() {
        return tag;
    }

    public void setTag(List<String> tag) {
        this.tag = tag;
    }

    public Integer getMaxWord() {
        return maxWord;
    }

    public void setMaxWord(Integer maxWord) {
        this.maxWord = maxWord;
    }

    public Integer getMinWord() {
        return minWord;
    }

    public void setMinWord(Integer minWord) {
        this.minWord = minWord;
    }

    public Date getExpire() {
        return expire;
    }

    public void setExpire(Date expire) {
        this.expire = expire;
    }

    public Boolean getFile() {
        return file;
    }

    public void setFile(Boolean file) {
        this.file = file;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public SearchDto(Integer startRow, Integer oneRowNum, String keyword, String category, String type, List<String> tag, Integer maxWord, Integer minWord, Date expire, Boolean file, String sort, Integer authorId) {
        this.startRow = startRow;
        this.oneRowNum = oneRowNum;
        this.keyword = keyword;
        this.category = category;
        this.type = type;
        this.tag = tag;
        this.maxWord = maxWord;
        this.minWord = minWord;
        this.expire = expire;
        this.file = file;
        this.sort = sort;
        this.authorId = authorId;
    }

    public SearchDto(RestrictionVo restrictionVo, String threeType){
        category=restrictionVo.getCategory();
        if (restrictionVo.getPage()==null){
            startRow=0;
        }else if (restrictionVo.getPage()<=0){
            throw new ServiceException("页码非法");
        }
        else {
            startRow = (restrictionVo.getPage() - 1) * oneRowNum;
        }
        keyword=restrictionVo.getKeyword();
        tag=restrictionVo.getTag();
        type=restrictionVo.getType();
        maxWord=restrictionVo.getMaxword();
        minWord=restrictionVo.getMinword();
        expire=restrictionVo.getExpire();
        file=restrictionVo.getFile();
        authorId= restrictionVo.getAuthor_id();
        switch (restrictionVo.getSort()){
            case 1: sort="like_number";break;
            case 2: sort="publish_date";break;
            default: {
                switch (threeType) {
                    case "article":
                    case "column":
                        sort = "read_number";
                        break;
                    case "source":
                        sort = "download_number";
                        break;
                    default:
                        throw new ServiceException("类型非法");
                }
                break;
            }
        }
    }
}
